package com.sptpc.practice.controller;

import com.sptpc.practice.controller.response.CommonReturnType;
import com.sptpc.practice.controller.viewobject.ItemVO;
import com.sptpc.practice.controller.viewobject.OrderVO;
import com.sptpc.practice.error.BusinessException;
import com.sptpc.practice.error.EmBusinessError;
import com.sptpc.practice.service.ItemService;
import com.sptpc.practice.service.OrderService;
import com.sptpc.practice.service.UserService;
import com.sptpc.practice.service.model.ItemModel;
import com.sptpc.practice.service.model.OrderModel;
import com.sptpc.practice.service.model.UserModel;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Controller("/order")
@RequestMapping("/order")
@CrossOrigin(origins = {"*"}, allowCredentials = "true")
public class OrderController extends BaseController{

    @Autowired
    private OrderService orderService;

    @Autowired
    private ItemService itemService;

    @Autowired
    private UserService userService;

    @Autowired
    private HttpServletRequest httpServletRequest;

    //封装下单请求
    @PostMapping(value = "/createorder",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ResponseBody
    public CommonReturnType createOrder(@RequestParam(name = "itemId") Integer itemId,
                                        @RequestParam(name = "amount") Integer amount,
                                        @RequestParam(name = "promoId",required = false) Integer promoId) throws BusinessException {

        Boolean isLogin = (Boolean) httpServletRequest.getSession().getAttribute("IS_LOGIN");
        if (isLogin == null || !isLogin.booleanValue()){
            throw new BusinessException(EmBusinessError.USER_NOT_LOGIN,"用户还未登录，不能下单");
        }
        //获取用户的登录信息
        UserModel userModel = (UserModel) httpServletRequest.getSession().getAttribute("LOGIN_USER");

        OrderModel orderModel = orderService.createOrder(userModel.getId(),itemId,promoId,amount);

        return CommonReturnType.create(null);
    }

    //订单列表浏览
    @GetMapping(value = "/list")
    @ResponseBody
    public CommonReturnType listOrder(){
        List<OrderModel> orderModelList = orderService.listOrder();

        List<OrderVO> orderVOList = orderModelList.stream().map(orderModel -> {
            OrderVO orderVO = this.convertVOFromModel(orderModel);
            return orderVO;
        }).collect(Collectors.toList());

        return CommonReturnType.create(orderVOList);
    }

    private OrderVO convertVOFromModel(OrderModel orderModel) {
        if (orderModel == null) {
            return null;
        }
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(orderModel, orderVO);
        orderVO.setName(userService.getUserById(orderModel.getUserId()).getName());
        orderVO.setTitle(itemService.getItemById(orderModel.getItemId()).getTitle());

        return orderVO;
    }

}
